/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class LineConstraint extends Node {

    //endpoints
    Point a, b;
    protected float length;


    public LineConstraint(Point a, Point b) {
        this.a = a;
        this.b = b;



        validateLength();

    }

    public boolean isSimilar(Point sa, Point sb){

        if (sa == a || sa == b){
            if (sb == a || sb == b){
                //shouldn't happen, but lets check anyway
                if (sa != sb && a != b){
                    return true;
                }
            }
        }
        return false;

    }

    public void physics() {

        float dx = b.pos.x - a.pos.x;
        float dy = b.pos.y - a.pos.y;
        float dz = b.pos.z - a.pos.z;

        float d1 = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);


        //find the mass divisor
        float arat = b.mass / (a.mass + b.mass);
        float brat = a.mass / (a.mass + b.mass);


        float d2 = (d1 - length) / d1;
        dx *= d2;
        dy *= d2;
        dz *= d2;

        // if (d1 > length){

        a.pos.x += dx * arat;
        a.pos.y += dy * arat;
        a.pos.z += dz * arat;

        b.pos.x -= dx * brat;
        b.pos.y -= dy * brat;
        b.pos.z -= dz * brat;
        //  }


    }

    public void render(int mode) {

        GL11.glLineWidth(1.5f);
        if (selected == 1) {
            GL11.glColor3f(1.0f, 1.0f, 0.0f);
        } else {
            GL11.glColor3f(red,green,blue);
        }
        GL11.glBegin(GL11.GL_LINE_LOOP);                    // Drawing Using Triangles
        GL11.glVertex3f(a.pos.x, a.pos.y, a.pos.z);
        GL11.glVertex3f(b.pos.x, b.pos.y, b.pos.z);
        GL11.glEnd();

    }

    public PVector getCentroid() {
        return new PVector( (b.pos.x - a.pos.x)/2 + a.pos.x,
                            (b.pos.y - a.pos.y)/2 + a.pos.y,
                            (b.pos.z - a.pos.z)/2 + a.pos.z);
    }



    public LineConstraint(String [] line, Vector <Node> nodes ){
        this((Point)nodes.elementAt(Integer.parseInt(line[1])),
             (Point)nodes.elementAt(Integer.parseInt(line[2])));
    }

    public PVector getScreenSpaceCentroid() {
        float[] ret = GLProject.project((b.pos.x - a.pos.x) / 2 + a.pos.x,
                (b.pos.y - a.pos.y) / 2 + a.pos.y,
                (b.pos.z - a.pos.z) / 2 + a.pos.z);

        return new PVector(ret[0], ret[1], 0);
    }



    public String pickle(Vector <Node> nodes) {
        return "L," +
                nodes.indexOf(a) + "," +
                nodes.indexOf(b) + ",";


    }

    public void validateLength(){
            PVector c = new PVector();
        PVector.sub(a.pos, b.pos, c);
        length = c.mag();
    }

        public boolean dependsOn(Point p) {
        if (p == a || p == b) {
            return true;
        } else {
            return false;
        }
    }

}
